using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Security;
using System.Security.Principal;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Administration
{
    public class ManageRolesPage : StarznetBaseAdministrationPage
    {
        protected WebApplicationSelector Selector;

        protected InputFormTextBox TxtDefaultRoleProvider;
        protected InputFormTextBox TxtApplicationName;
        protected InputFormCheckBox ChkEnableRoles;
        protected DropDownList DdlConnString;
        protected Label LblWebApp;
        protected Panel PnlOtherConn;
        protected InputFormTextBox TxtOtherConn;

        private String sWebApp;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            sWebApp = Request.QueryString["wapp"];

            DdlConnString.SelectedIndexChanged += new EventHandler(DdlConnString_SelectedIndexChanged);

            if(!IsPostBack)
                this.LoadRoleSettings();

        }

        void DdlConnString_SelectedIndexChanged(object sender, EventArgs e)
        {
            PnlOtherConn.Visible = (DdlConnString.SelectedValue == string.Empty);
        }

        private void LoadRoleSettings()
        {
            IntPtr oUserToken = WindowsIdentity.GetCurrent().Token;

            System.Configuration.Configuration oConfig =
                WebConfigurationManager.OpenWebConfiguration("/", sWebApp, "", Environment.MachineName, oUserToken);
            RoleManagerSection oRoleSection = (RoleManagerSection)oConfig.GetSection("system.web/roleManager");

            if (oRoleSection == null) return;
            
            ProviderSettings oSettings = oRoleSection.Providers[oRoleSection.DefaultProvider];

            //Populate the connection string dropdown
            ListItem oItem;
            foreach (ConnectionStringSettings oConn in oConfig.ConnectionStrings.ConnectionStrings)
            {
                oItem = new ListItem(oConn.Name, oConn.Name);
                this.DdlConnString.Items.Add(oItem);
            }
            this.DdlConnString.Items.Add(new ListItem("Specify Other", string.Empty));
            this.DdlConnString.SelectedValue = oSettings.Parameters["connectionStringName"].ToString();
            this.LblWebApp.Text = sWebApp;

            this.TxtDefaultRoleProvider.Text = oRoleSection.DefaultProvider;
            this.TxtApplicationName.Text = oSettings.Parameters["applicationName"].ToString();
            this.ChkEnableRoles.Checked = oRoleSection.Enabled;
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            IntPtr oUserToken = WindowsIdentity.GetCurrent().Token;

            System.Configuration.Configuration oConfig =
                WebConfigurationManager.OpenWebConfiguration("/", sWebApp, "", Environment.MachineName, oUserToken);
            RoleManagerSection oRoleSection = (RoleManagerSection)oConfig.GetSection("system.web/roleManager");

            if (oRoleSection == null && this.ChkEnableRoles.Checked)
            {
                //Create the section
                oConfig.Sections.Add("roleManager", new RoleManagerSection());
            }

            ProviderSettings oSettings = oRoleSection.Providers[oRoleSection.DefaultProvider];

            oRoleSection.Enabled = this.ChkEnableRoles.Checked;
            if (DdlConnString.SelectedValue == string.Empty)
            {
                //Add a new connection string
                Guid oGuid = Guid.NewGuid();
                ConnectionStringSettings oConnSettings = new ConnectionStringSettings();
                oConnSettings.Name = "SZ_" + oGuid.ToString().Replace("-", "");
                oConnSettings.ConnectionString = this.TxtOtherConn.Text;
                oConfig.ConnectionStrings.ConnectionStrings.Add(oConnSettings);

                oSettings.Parameters["connectionStringName"] = oConnSettings.Name;
            }
            else
                oSettings.Parameters["connectionStringName"] = this.DdlConnString.SelectedValue; 
            
            oSettings.Parameters["applicationName"] = this.TxtApplicationName.Text;

            oConfig.Save();

            Response.Redirect(PageToRedirectOnCancel);
        }

        public override string PageToRedirectOnCancel
        {
            get
            {
                return "MembershipAppList.aspx";
            }
        }
    }
}
